--[[
    函数：lua2_safe_switch_db_v1_1
    功能：切换search_db和cache_db，切换之前判断buildCacheStart和buildCacheEnd是否存在，存在时才切换；切换完成后清除这两个值
    返回：返回：
            0:code,0表示成功，其它表示中途退出
--]]

-- ########################################################

-- 分类的前缀
local PREFIX_TYPE = "type_"
-- 标签的前缀
local PREFIX_LABEL = "label_"
-- VIP的key
local KEY_VIP = "vip"
-- 排序的key
local KEY_SORT = "sort"
-- keyword的前缀
local PREFIX_KEYWORD = "kwd_"
-- ID-模版缓存的前缀
local PREFIX_DESIGN = "d_"

-- PPT的ID的前缀
local PREFIX_PPT = "p"
-- key的连接符号
local PREFIX_S = "*"
-- 数组字符串的分割符号
local PREFIX_ARRAY_SPLIT = ","

-- search_db
local SEARCH_DB = "search_db"
-- search_cache_db
local SEARCH_CACHE_DB = "search_cache_db"

-- buildCacheStart
local BUILD_CACHE_START = "buildCacheStart"
-- buildCacheEnd
local BUILD_CACHE_END = "buildCacheEnd"


-- ########################################################
local ret = 1
local s = redis.call('get',BUILD_CACHE_START)
local e = redis.call('get',BUILD_CACHE_END)


if((s) and (e))
then
    -- 切换db
    local sdb = redis.call('get',SEARCH_DB)
    local sdb_ = tonumber(sdb) or 0
    if(sdb_==1)
    then
        string.set_search_config(SEARCH_DB, 2, SEARCH_CACHE_DB, 1)
    else
        string.set_search_config(SEARCH_DB, 1, SEARCH_CACHE_DB, 2)
    end

    -- 删除 时间
    redis.call('del',BUILD_CACHE_START)
    redis.call('del',BUILD_CACHE_END)

    ret = 0
end




-- ########################################################
return ret